import Vue from 'vue';
import Vuex from 'vuex';
import user from './modules/user';
import friend from './modules/friend';
import group from './modules/group';
import videocall from './modules/videocall';
import conversation from './modules/conversation';
import news from './modules/news';
import todo from './modules/todo';
import { Message } from 'element-ui';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    current: Date.now(), // 当前时间
    intervalID: 0,
    message: undefined
  },
  getters: {
    // 判断dom是否获得焦点
    // 场景：当前会话自动已读
    hidden(state) {
      // 这里不加state不会正常返回
      state.current;
      if (typeof document.hasFocus !== 'function') {
        return document.hidden;
      }
      return !document.hasFocus();
    }
  },
  mutations: {
    startComputeCurrent(state) {
      state.intervalID = setInterval(() => {
        state.current = Date.now();
      }, 1000)
    },
    stopComputeCurrent(state) {
      clearInterval(state.intervalID);
      state.intervalID = 0;
    },
    showMessage(state, options) {
      if (state.message) {
        state.message.close();
      }
      state.message = Message({
        message: options.message,
        type: options.type || 'success', // success/warning/info/error	
        duration: options.duration || 2000,
        offset: 40
      });
    }
  },
  modules: {
    user,
    friend,
    group,
    conversation,
    videocall,
    news,
    todo
  }
});